﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using ChanExt;
using C.Unite;
using C.Entities;

namespace ArtLand.OA.Project.Project
{
    public partial class Pro_PopayfChange : CommonPageBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                DataBaseHelper.BindDrp(Drp_PayNo, EnumData.DataBaseEunm.PayNO);
                FormInfo.ShowTriggerWin(Window2, txt_PONUBR, hid_PONUBR, "/PublicModel/SelectPodata.aspx", "选择采购订单");
                btnClose.OnClientClick = ActiveWindow.GetHideReference();
                LoadData();
            }
        }

        protected void LoadData()
        {
            if (!string.IsNullOrEmpty(Request["pid"]))
            {
                Proj_Podata pod = TbsDbSession.Default.From<Proj_Podata>().Where(Proj_Podata._.ID == int.Parse(Request["pid"])).ToFirst();
                txt_PONUBR.Text = pod.PONBER;
                if (pod.TAMOUT.HasValue)
                    txt_ARAMOT.Text = pod.TAMOUT.Value.ToString();
                else
                    txt_ARAMOT.Text = "0";
                txt_PONUBR.Enabled = false;

                SetT();

                //if (pod.CHKTAX.Value)
                //    lblIsShui.Text = "&nbsp;&nbsp;&nbsp;&nbsp;<font color='red'><b>已含税</b></font>";
                //else
                //    lblIsShui.Text = "&nbsp;&nbsp;&nbsp;&nbsp;<b>不含税</b>";
            }


            if (!string.IsNullOrEmpty(Request["id"]))
            {
                VI_Popayf Popay = TbsDbSession.Default.From<VI_Popayf>().Where(VI_Popayf._.ID == int.Parse(Request["id"])).ToFirst();
                if (Popay != null)
                {
                    txt_PONUBR.Text = Popay.PONUBR;
                    txt_PONUBR.Enabled = false;
                    if (Popay.ARAMOT.HasValue)
                        txt_ARAMOT.Text = Popay.ARAMOT.Value.ToString();
                    if (Popay.IAMOUT.HasValue)
                        txt_IAMOUT.Text = Popay.IAMOUT.Value.ToString();
                    if (Popay.INDATE.HasValue)
                        txt_INDATE.Text = Popay.INDATE.Value.ToString("yyyy-MM-dd");
                    txt_INVNUB.Text = Popay.INVNUB;
                    if (Popay.REMOUT.HasValue)
                        txt_REMOUT.Text = Popay.REMOUT.Value.ToString();
                    txt_PERSON.Text = Popay.PERSON;
                    if (Popay.REDATE.HasValue)
                        txt_REDATE.Text = Popay.REDATE.Value.ToString("yyyy-MM-dd");
                    txt_REMARK.Text = Popay.REMARK;
                    Drp_INSTAS.SelectedValue = Popay.INSTAS;
                    Drp_PayNo.SelectedValue = Popay.PayNo;


                    string pono = Popay.PONUBR;
                    decimal z = decimal.Parse(Popay.ARAMOT.Value.ToString());
                    decimal j = 0;
                    List<Proj_Popayf> popayfList = TbsDbSession.Default.From<Proj_Popayf>().Where(Proj_Popayf._.PONUBR == pono).ToList();
                    foreach (Proj_Popayf item in popayfList)
                    {
                        j += item.REMOUT.Value;
                    }
                    decimal y = z - j;


                    //if (Popay.Sheng.HasValue)
                    txt_Sheng.Text = y.ToString();// Popay.Sheng.Value.ToString();
                    if (Popay.CHKTAX.Value)
                        lblIsShui.Text = "&nbsp;&nbsp;&nbsp;&nbsp;<font color='red'><b>已含税</b></font>";
                    else
                        lblIsShui.Text = "&nbsp;&nbsp;&nbsp;&nbsp;<b>不含税</b>";
                }
            }
        }


        protected void btnSave_Click(object sender, EventArgs e)
        {
            bool f = true;
            if (Drp_INSTAS.SelectedValue != "未开票")
            {
                if (txt_INVNUB.Text == "")
                {
                    txt_INVNUB.MarkInvalid(String.Format("请为发票号提供有效值", txt_INVNUB.Text));
                    Alert.ShowInTop("请为发票号提供有效值");
                    f = false;
                }
                if (txt_IAMOUT.Text == "")
                {
                    txt_IAMOUT.MarkInvalid(String.Format("请为开票金额提供有效值", txt_IAMOUT.Text));
                    Alert.ShowInTop("请为开票金额提供有效值");
                    f = false;
                }
                if (txt_INDATE.Text == "")
                {
                    txt_INDATE.MarkInvalid(String.Format("请为开票日期提供有效值", txt_INDATE.Text));
                    Alert.ShowInTop("请为开票日期提供有效值");
                    f = false;
                }
            }
            if (f)
            {
                int returnvalue = 0;
                Proj_Popayf Popay = new Proj_Popayf();
                if (!string.IsNullOrEmpty(Request["id"]))
                {
                    Popay.ID = int.Parse(Request["id"]);
                    Popay.Attach();
                    Popay.ARAMOT = Convert.ToDecimal(txt_ARAMOT.Text);
                    Popay.PONUBR = txt_PONUBR.Text;
                    if (txt_IAMOUT.Text != "")
                        Popay.IAMOUT = Convert.ToDecimal(txt_IAMOUT.Text);
                    if (txt_INDATE.Text != "")
                        Popay.INDATE = Convert.ToDateTime(txt_INDATE.Text);
                    Popay.INVNUB = txt_INVNUB.Text;
                    Popay.REMOUT = Convert.ToDecimal(txt_REMOUT.Text);
                    Popay.PERSON = txt_PERSON.Text;
                    Popay.REDATE = Convert.ToDateTime(txt_REDATE.Text);
                    Popay.REMARK = txt_REMARK.Text;
                    Popay.REMARK = txt_REMARK.Text;
                    Popay.INSTAS = Drp_INSTAS.SelectedValue;
                    Popay.PayNo = Drp_PayNo.SelectedValue;
                    Popay.Sheng = Convert.ToDecimal(txt_Sheng.Text);
                    returnvalue = TbsDbSession.Default.Update<Proj_Popayf>(Popay);
                }
                else
                {
                    Popay.DeAttach();
                    Popay.ARAMOT = Convert.ToDecimal(txt_ARAMOT.Text);
                    Popay.PONUBR = txt_PONUBR.Text;
                    if (txt_IAMOUT.Text != "")
                        Popay.IAMOUT = Convert.ToDecimal(txt_IAMOUT.Text);
                    if (txt_INDATE.Text != "")
                        Popay.INDATE = Convert.ToDateTime(txt_INDATE.Text);
                    Popay.INVNUB = txt_INVNUB.Text;
                    Popay.REMOUT = Convert.ToDecimal(txt_REMOUT.Text);
                    Popay.PERSON = txt_PERSON.Text;
                    Popay.REDATE = Convert.ToDateTime(txt_REDATE.Text);
                    Popay.REMARK = txt_REMARK.Text;
                    Popay.REMARK = txt_REMARK.Text;
                    Popay.Adder = CookInfo.EAdminTrueName();
                    Popay.AddID = CookInfo.EAdminid();
                    Popay.AddTime = DateTime.Now;
                    Popay.INSTAS = Drp_INSTAS.SelectedValue;
                    Popay.PayNo = Drp_PayNo.SelectedValue;
                    Popay.Sheng = Convert.ToDecimal(txt_Sheng.Text);
                    returnvalue = TbsDbSession.Default.Insert<Proj_Popayf>(Popay);
                }
                if (returnvalue > 0)
                {
                    Alert.Show("更新成功！", "提示", ActiveWindow.GetHideRefreshReference());
                }
            }
        }

        protected void Window2_Close(object sender, WindowCloseEventArgs e)
        {
            SetT();
        }

        protected void SetT()
        {
            if (txt_PONUBR.Text != "")
            {
                Proj_Podata po = TbsDbSession.Default.From<Proj_Podata>().Where(Proj_Podata._.PONBER == txt_PONUBR.Text).ToFirst();
                decimal tt = po.TAMOUT.Value;
                decimal cc = 0;
                List<Proj_Popayf> popay = TbsDbSession.Default.From<Proj_Popayf>().Where(Proj_Popayf._.PONUBR == po.PONBER).ToList();
                foreach (Proj_Popayf item in popay)
                {
                    cc += item.REMOUT.Value;
                }
                txt_Sheng.Text = (tt - cc).ToString();
                txt_ARAMOT.Text = tt.ToString();// po.TAMOUT.Value.ToString();
                if (po.CHKTAX.Value)
                    lblIsShui.Text = "&nbsp;&nbsp;&nbsp;&nbsp;<font color='red'><b>已含税</b></font>";
                else
                    lblIsShui.Text = "&nbsp;&nbsp;&nbsp;&nbsp;<b>不含税</b>";
            }
        }
    }
}